package data_structure.leetcode;

public class Solution875 {

    public int minEatingSpeed(int[] piles, int h) {
        int min=1;
        int max = 1;
        for(int i=0;i<piles.length; i++){
            if(piles[i]>max){
                max=piles[i];
            }
        }

        int mid;
        while(min<max){
            mid=min+(max-min)/2;
            if(eatTime (piles,mid)>h){
                min=mid+1;
            }else{
                max=mid;
            }
        }
        return min;

    }


    private int eatTime(int []piles,int speed){

        int result=0;
        for(int pile:piles){
           int time=pile%speed==0?pile/speed:pile / speed+1;
           result+=time;
        }
        return result;
    }
}
